package com.java.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java.business.dao.CheckinMapper;
import com.java.business.dao.MaintainRecordMapper;
import com.java.business.model.Appointment;
import com.java.business.model.Checkin;
import com.java.business.model.MaintainRecord;
import com.java.business.service.CheckinService;
import com.java.business.service.MaintainRecordService;
import com.java.business.service.api.CheckinServiceApi;
import com.java.common.model.Result;
import com.java.manage.model.Accessories;
import com.java.manage.model.Company;
import com.java.manage.model.Maintain;
import com.java.manage.service.api.AccessoriesApiService;
import com.java.manage.service.api.CompanyApiService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class MaintainRecordServiceImpl implements MaintainRecordService {

    @Autowired
    private MaintainRecordMapper maintainRecordMapper;

    @DubboReference
    private CompanyApiService companyApiService;

    @Autowired
    private CheckinService checkinService;

    /**
     * 关联配件表
     */
    @DubboReference
    private AccessoriesApiService accessoriesApiService;

    @DubboReference
    private CheckinServiceApi checkinServiceApi;

    @Override
    public Result list(MaintainRecord maintainRecord) {
        PageHelper.startPage(maintainRecord.getPageNum(), maintainRecord.getPageSize());
        List<MaintainRecord> list = maintainRecordMapper.list(maintainRecord);

        // 门店表
        List<Integer> storeCollect = list.stream().map(MaintainRecord::getStoreId).collect(Collectors.toList());
        List<Company> companies = companyApiService.listByIds(storeCollect);
        Map<Integer, Company> companyMap = companies.stream().collect(Collectors.toMap(store -> store.getId(), store -> store));

        list.stream().forEach(app -> {
            app.setStore(companyMap.get(app.getStoreId()));
        });

        PageInfo<MaintainRecord> pageInfo = new PageInfo<>(list);

        Map<String, Object> map = new HashMap<>();
        map.put("list", pageInfo.getList());
        map.put("total", pageInfo.getTotal());
        return Result.success(map);
    }

    @Override
    public Result save(MaintainRecord maintainRecord) {
        maintainRecord.setCreateTime(new Date());
        maintainRecord.setSignStatus("2");
        maintainRecord.setPickingStatus("1");
        maintainRecordMapper.save(maintainRecord);
        return Result.success();
    }

    @Override
    public Result modify(MaintainRecord maintainRecord) {
        maintainRecordMapper.modify(maintainRecord);
        return Result.success();
    }

    @Override
    public Result remove(int[] ids) {
        for (int id : ids) {
            maintainRecordMapper.remove(id);
        }
        return Result.success();
    }

    @Override
    public Result get(int id) {
        maintainRecordMapper.get(id);
        return Result.success();
    }

    @Override
    public Result listAll() {
        List<MaintainRecord> companies = maintainRecordMapper.listAll();
        return Result.success(companies);
    }

    @Override
    public Result listRecord(MaintainRecord maintainRecord) {
        PageHelper.startPage(maintainRecord.getPageNum(), maintainRecord.getPageSize());
        List<MaintainRecord> list = maintainRecordMapper.list(maintainRecord);
        for (MaintainRecord record : list) {
            // 登记表
            Checkin checkin = checkinService.get(record.getCheckinId());
            record.setCheckin(checkin);
            Accessories accessories = accessoriesApiService.listAll(record.getCheckinId());
            record.setAccessories(accessories);
        }
        PageInfo<MaintainRecord> pageInfo = new PageInfo<>(list);

        Map<String, Object> map = new HashMap<>();
        map.put("list", pageInfo.getList());
        map.put("total", pageInfo.getTotal());
        return Result.success(map);
    }

    @Override
    public Result lists(Integer id) {

        List<MaintainRecord> list = maintainRecordMapper.lists(id);

        // 门店表
        List<Integer> storeCollect = list.stream().map(MaintainRecord::getStoreId).collect(Collectors.toList());
        List<Company> companies = companyApiService.listByIds(storeCollect);
        Map<Integer, Company> companyMap = companies.stream().collect(Collectors.toMap(store -> store.getId(), store -> store));

        list.stream().forEach(app -> {
            app.setStore(companyMap.get(app.getStoreId()));
        });

        PageInfo<MaintainRecord> pageInfo = new PageInfo<>(list);

        Map<String, Object> map = new HashMap<>();
        map.put("list", pageInfo.getList());
        map.put("total", pageInfo.getTotal());
        return Result.success(map);
    }

}
